package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemServiceHelper;
import id.ac.amikom.libs.CustomAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AdemExploreMateriActivity extends ListActivity {

	private String mode;
	private String param;
	private String[] nik;
	private String[] kode;
	private String[] seq;
	private String[] file;
	private JSONArray jsonArray;
	private ListAdapter adapter;

	private class GetDataTask extends AsyncTask<Void, Void, Boolean> {

		ProgressDialog dialog = new ProgressDialog(
				AdemExploreMateriActivity.this);
		boolean result = false;

		@Override
		protected Boolean doInBackground(Void... params) {

			AdemServiceHelper srv = new AdemServiceHelper(
					AdemExploreMateriActivity.this);

			jsonArray = srv.getMateri(mode, param);
			result = true;

			return result;

		}

		protected void onPreExecute() {
			this.dialog.setMessage("Loading Materi..");
			this.dialog.show();
		}

		@Override
		protected void onPostExecute(Boolean result) {

			if (result)
				setList();
			else {

				Toast.makeText(AdemExploreMateriActivity.this,
						"No Internet Connection", Toast.LENGTH_LONG).show();

				finish();
			}

			this.dialog.dismiss();
		}

	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.adem_title_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.adem_title);

		Bundle extras = getIntent().getExtras();
		mode = extras.getString("mode");

		if (mode.equals("mkl")) {
			String[] data = extras.getString("param").split("\\(+");
			param = data[0];
		} else
			param = extras.getString("param");

		new GetDataTask().execute();

	}

	public void setList() {
		try {

			ArrayList<HashMap<String, Object>> newsList = new ArrayList<HashMap<String, Object>>();

			nik = new String[jsonArray.length()];
			kode = new String[jsonArray.length()];
			seq = new String[jsonArray.length()];
			file = new String[jsonArray.length()];

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject json = jsonArray.getJSONObject(i);
				HashMap<String, Object> map = new HashMap<String, Object>();

				nik[i] = json.getString("nik").toString().trim();
				kode[i] = json.getString("kode").toString().trim();
				seq[i] = json.getString("Materike").toString().trim();
				file[i] = json.getString("namafile").toString().trim();

				String[] f = file[i].split("[/]");
				String t[] = f[2].split("[.]");
				String type = t[t.length - 1];

				if (type.equals("doc") || type.equals("docx"))
					map.put("icon", R.drawable.i_word);
				else if (type.equals("xls") || type.equals("xlsx"))
					map.put("icon", R.drawable.i_excel);
				else if (type.equals("ppt") || type.equals("pptx"))
					map.put("icon", R.drawable.i_ppt);
				else if (type.equals("pdf"))
					map.put("icon", R.drawable.i_pdf);
				else
					map.put("icon", R.drawable.i_other);

				map.put("dosen",
						json.getString("nama") + "\n" + json.getString("mkl"));
				map.put("judul", json.getString("judul"));
				newsList.add(map);

			}

			adapter = new CustomAdapter(this, newsList, R.layout.adem_list_img,
					new String[] { "judul", "dosen", "icon" }, new int[] {
							R.id.list_title, R.id.list_date, R.id.list_image });

			setListAdapter(adapter);
			registerForContextMenu(getListView());

		} catch (JSONException e) {
			e.printStackTrace();

		}
	}

	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Intent i = new Intent(AdemExploreMateriActivity.this,
				AdemDetailMateriActivity.class);
		i.putExtra("_key", nik[position] + "-" + kode[position] + "-"
				+ seq[position]);
		i.putExtra("_id", id);

		startActivity(i);
		finish();
	}

}
